import { App } from "../../application";
import Const from "../../application/constants/Const";
import { Point } from "./Point";

/** 屏幕像素类 */
export default class Pixel {
    public x: number;
    public y: number;
    public z: number;
    constructor(x: number, y: number, z: number = 0) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public getX() {
        return this.x;
    }
    public getY() {
        return this.y;
    }
    public getZ() {
        return this.z;
    }
    public equals(point: Pixel): boolean {
        return this.x === point.getX() && this.y === point.getY() && this.z === point.getZ();
    }
    public offset(x: number, y: number, z: number = 0) {
        this.x += x;
        this.y += y;
        this.z += z;
        return this;
    }
    public set(x: number, y: number, z: number = 0) {
        this.x = x;
        this.y = y;
        this.z = z;
        return this;
    }
    public add(px: Pixel) {
        if (!px) return;
        this.x += px.getX();
        this.y += px.getY();
        this.z += px.getZ();
        return this;
    }
    public sub(px: Pixel) {
        if (!px) return;
        this.x -= px.getX();
        this.y -= px.getY();
        this.z -= px.getZ();
        return this;
    }
    public round() {
        this.x = Math.round(this.x);
        this.y = Math.round(this.y);
        this.z = Math.round(this.z);
        return this;
    }
    public clone() {
        return new Pixel(this.x, this.y, this.z);
    }
    public copy(px: Pixel) {
        this.x = px.getX();
        this.y = px.getY();
        this.z = px.getZ();
    }
    public toString(): string {
        return this.x + ',' + this.y + ',' + this.z;
    }

    public toMecator(): Point {
        let x = this.x;
        let y = this.y;
        let z = 0.5;
        let app = App.getInstance();
        let camera = app.three.camera;
        let size = app.getSize();
        let mouse = new Point((x / size.width) * 2 - 1, -(y / size.height) * 2 + 1, z, Const.EPSGType.EPSGWeb);
        camera.updateMatrixWorld();
        camera.updateProjectionMatrix();
        mouse.unproject(camera);
        return mouse;
    };
}